﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using WebSite.ViewModels.Fields;
using WebSite.Views;

namespace WebSite.ViewModels.EditViewModels
{
    public class UsuarioEditViewModel : EditViewModel
    {
        private readonly IAreaOperacaoRegistry m_AreaOperacaoRegistry;
        private readonly Representante m_Representante;

        public UsuarioEditViewModel(IAreaOperacaoRegistry areaOperacaoRegistry, Representante representante)
            : this()
        {
            m_AreaOperacaoRegistry = areaOperacaoRegistry;
            m_Representante = representante;
        }

        public UsuarioEditViewModel(string message = null)
            : base("Usuário", message)
        {
        }

        protected override IEnumerable<IField> GetGeralFields()
        {
            User user = (User) Model;

            List<IField> result = new List<IField>
            {
                new TextBoxField("Username", "username", user.Username, true),
                new PasswordField("Password", "password", user.Password),
                new TextBoxField("E-mail", "email", user.Email),
                new CheckBoxField("Ativo", "ativo", user.Ativo)
            };
            result.AddRange(base.GetGeralFields());
            return result;
        }

        public bool IsOperacaoRelevant(Operacao operacao)
        {
            return m_AreaOperacaoRegistry.IsOperacaoRelevant(operacao, m_Representante);
        }

        public bool IsAreaRelevant(Area area)
        {
            return m_AreaOperacaoRegistry.IsAreaRelevant(area, m_Representante);
        }

        public IEnumerable<Operacao> GetOperacoes(Area area)
        {
            return m_AreaOperacaoRegistry.GetOperacoes(area);
        }

        public Permissao GetPermissao(Area area)
        {
            User user = (User) Model;
            return user.Permissoes.FirstOrDefault(x => x.Area == area);
        }

        protected override IEnumerable<TabItem> GetTabs(HtmlHelper htmlHelper)
        {
            List<TabItem> result = base.GetTabs(htmlHelper).ToList();

            User user = (User)Model;

            result.Add(new TabItem("Permissões",
                "~/Content/permissions.png",
                htmlHelper.Partial("PermissaoEdit", this)));

            result.Add(new TabItem("Filtros",
                "~/Content/filter.png",
                GetEditField<Fabrica>(htmlHelper, user, "Fábrica(s)", "fabricaFiltro", "SearchFabrica", x => x.Nome).ToHtmlString() +
                GetEditField<Cliente>(htmlHelper, user, "Cliente(s)", "clienteFiltro", "SearchClienteToken", x => x.RazaoSocial).ToHtmlString() +
                GetEditField<Vendedor>(htmlHelper, user, "Vendedor(es)", "vendedorFiltro", "SearchVendedor", x => x.Nome).ToHtmlString() +
                GetEditField<Transportadora>(htmlHelper, user, "Transportadora(s)", "transportadoraFiltro", "SearchTransportadora", x => x.Nome).ToHtmlString()));
            
            return result;
        }

        private static IHtmlString GetEditField<TModel>(HtmlHelper htmlHelper, User user, string caption, string name, string actionName, Func<TModel, string> getName)
            where TModel : UsuariosHolder
        {
            IEnumerable<TModel> filtro = user.Filtros.OfType<TModel>();
            return htmlHelper.FieldFor(caption, new TokenInputField(caption, name, String.Join(",", filtro.Select(x => x.Id.ToString())), "Search", actionName, filtro.ToDictionary(x => x.Id, getName)).Render(htmlHelper));
        }
    }
}